package com.sinodata.bsm.cicp.collector.weblogic8;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-26 AM 10:04:36   liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ServerInfoCollector extends Collector {

    private static final Logger logger = Logger.getLogger(ServerInfoCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        String port = this.getParamMap().get("Port").trim();//端口
        String username = this.getParamMap().get("Username").trim();//用户名
        String password = this.getParamMap().get("Password").trim();//密码
        String serverName = this.getParamMap().get("Name").trim();//名称
        // List<String> keys = new ArrayList<String>();
        //List<String[]> values = new ArrayList<String[]>();
        String xmlString = XmlGetter.getJmxXml("WlgServerInfo", ip, port, username, password);
        try {
            // parse xml
            Document document = DocumentHelper.parseText(xmlString.toString());
            Element rootElement = document.getRootElement();
            Iterator<?> iterator = rootElement.elementIterator("ServerInfo");
            boolean isDown = true;
            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
            while (iterator.hasNext()) {
                Element element = (Element) iterator.next();
                if (element != null) {
                    String name = element.element("Name").getTextTrim();
                    if (name.equals(serverName)) {
                        isDown = false;
                        String weblogicVersion = element.element("WeblogicVersion").getTextTrim();
                        String state = element.element("State").getTextTrim();
                        String listenPort = element.element("ListenPort").getTextTrim();
                        String socketsOpenedTotalCount = element.element("SocketsOpenedTotalCount").getTextTrim();
                        String openSocketsCurrentCount = element.element("OpenSocketsCurrentCount").getTextTrim();
                        String activationTime = element.element("ActivationTime").getTextTrim();
                        propValues[0] = ResPropertyValue.createPropValue(weblogicVersion, propMetas[0]);
                        propValues[1] = ResPropertyValue.createPropValue(state, propMetas[1]);
                        propValues[2] = ResPropertyValue.createPropValue(listenPort, propMetas[2]);
                        logger.debug("----activationTime---" + activationTime);
                        propValues[3] = ResPropertyValue.createPropValue(activationTime, propMetas[3]);
                        propValues[4] = ResPropertyValue.createPropValue(socketsOpenedTotalCount, propMetas[4]);
                        propValues[5] = ResPropertyValue.createPropValue(openSocketsCurrentCount, propMetas[5]);
                        break;
                        //                     propertyValueX.setValues(propValues);
                        //                     values.add(propertyValueX);
                    }
                    /*values.add(new String[] { weblogicVersion, state,
                      listenPort, activationTime,
                      socketsOpenedTotalCount, openSocketsCurrentCount });*/
                }
            }

            if (isDown) {
                propValues[0] = ResPropertyValue.createPropValue("N/A", propMetas[0]);
                propValues[1] = ResPropertyValue.createPropValue("SHUTDOWN", propMetas[1]);
                propValues[2] = ResPropertyValue.createPropValue("N/A", propMetas[2]);
                propValues[3] = ResPropertyValue.createPropValue("N/A", propMetas[3]);
                propValues[4] = ResPropertyValue.createPropValue("N/A", propMetas[4]);
                propValues[5] = ResPropertyValue.createPropValue("N/A", propMetas[5]);
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (DocumentException e) {
            logger.error("failed to collect by ServerInfoCollector", e);
            throw new CollectorException(e);
        }

    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "端口", "用户名", "密码", "名称" };
        return new String[] { "Port", "Username", "Password", "Name" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "版本", "状态", "监听端口", "激活时间", "已打开的套接字总数", "当前打开的套接字数" };
        return new String[] { "Version", "Status", "MonitorPort", "ActivateTime", "TotalOpenedSocketsNum", "CurrentOpenedSocketsNum" };
    }

}